import os
from time import sleep

import allure
import pytest

import page
from page.page_discovery import PageDiscovery
from page.page_login import PageLogin
from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("发现页面")
class TestDiscovery:

    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.dis = PageDiscovery(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        cls.login.pagelogin(os.environ['DISCOVERY_USERNAME'], password='123456')
        # cls.login.pagelogin('11100009235', password='123456')
        cls.login.base_click_element(page.wd_tab)
        cls.dis.pagediscovery_click_discovery_tab()

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    @allure.story("分享文章到qq用例")
    def test01_discovery_share_content(self):
        self.dis.pagediscovery_click_discovery_tab()
        self.dis.pagediscovery_click_card_content_1()
        self.dis.pagediscovery_click_share()
        self.dis.pagediscovery_click_share_to_qq()
        try:
            self.dis.pagediscovery_click_share_permission()
            self.dis.pagediscovery_click_share()
            self.dis.pagediscovery_click_share_to_qq()
        except:
            pass

        try:
            sleep(3)
            assert self.dis.pagediscovery_if_share_success()
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '分享文章到qq失败')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()

    @allure.story("文章点赞用例")
    def test02_discovery_dianzan(self):
        self.dis.pagediscovery_click_discovery_tab()
        self.dis.pagediscovery_click_card_content_1()
        if self.dis.pagediscovery_swipe_find_dianzan():
            number1 = self.dis.pagediscovery_get_dianzan_number()
            self.dis.pagediscovery_click_dianzan()
            sleep(3)
            number2 = self.dis.pagediscovery_get_dianzan_number()
        else:
            self.dis.base_get_screenshot_as_file(module_name, '本文章未有评论，无法点赞')
            assert False
        try:
            assert int(number2) - int(number1) == 1
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '点赞失败')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()

    @allure.story("AI学习用例——形容词")
    def test03_discovery_ai_adj(self):
        self.dis.pagediscovery_click_ai_study()
        self.dis.pagediscovery_click_ai_adj()
        try:
            assert self.dis.pagediscovery_if_adj_success()
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '形容词AI学习出错')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()

    @allure.story("AI学习用例——地点名词")
    def test04_discovery_ai_addr(self):
        self.dis.pagediscovery_click_ai_study()
        self.dis.pagediscovery_click_ai_addr()
        try:
            assert self.dis.pagediscovery_if_addr_success()
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '地点名词AI学习出错')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_ONLINE'])), reason='跳过正式站测试开关打开，跳过正式站用例')
    @allure.story("发现页banner图跳转测试")
    def test05_discovery_banner(self):
        self.dis.pagediscovery_click_banner()
        try:
            assert self.dis.pagediscovery_if_banner_success()
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '发现页banner跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_ONLINE'])), reason='跳过正式站测试开关打开，跳过正式站用例')
    @allure.story("发现页广告位1跳转测试")
    def test06_discovery_ad_1(self):
        self.dis.pagediscovery_click_ad_1()
        try:
            assert self.dis.pagediscovery_if_ad_1_success()
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '发现页广告位1跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()

    @allure.story("发现页音频播放测试")
    def test07_discovery_audio(self):
        self.dis.pagediscovery_click_audio()
        try:
            assert self.dis.pagediscovery_if_audio_success()
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '音频播放失败')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_ONLINE'])), reason='跳过正式站测试开关打开，跳过正式站用例')
    @allure.story("发现页广告位2跳转测试")
    def test08_discovery_ad_2(self):
        self.dis.pagediscovery_go_down()
        self.dis.pagediscovery_click_ad_2()
        try:
            assert self.dis.pagediscovery_if_ad_2_success()
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '发现页广告位2跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_ONLINE'])), reason='跳过正式站测试开关打开，跳过正式站用例')
    @allure.story("发现页广告位3跳转测试")
    def test09_discovery_ad_3(self):
        self.dis.pagediscovery_go_down()
        self.dis.pagediscovery_click_ad_3()
        try:
            assert self.dis.pagediscovery_if_ad_3_success()
            log.info('断言成功')
            self.dis.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.dis.base_get_screenshot_as_file(module_name, '发现页广告位3跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.dis.pagediscovery_return_to_discoverypage_keycode()
